class PersonOrder < ActiveRecord::Base
  has_many :tickets, :class_name => 'PersonTicket', :foreign_key => 'web_order_id'
  belongs_to :web_user
  
  def self.getNewPersonOrderNo()
    begin
      now = Time.new
      order_no = "T"+now.strftime("%Y%m%d")
      order = PersonOrder.find(:first,:conditions=>"order_no is not null and order_no like '"+order_no+"%'",:order=>"id desc")
      if order && order.order_no
        no = order.order_no
        key = no[9,5].to_i+1
        if key<10
          key = "0000"+key.to_s
        elsif key<100
          key = "000"+key.to_s
        elsif key<1000
          key = "00"+key.to_s
        elsif key<10000
          key = "0"+key.to_s
        else
          key = key.to_s
        end
      else
        key = "00001"
      end
      order_no << key
      return order_no
    rescue Exception => e
      logger.error("model PersonOrder getNewPersonOrderNo e="+e.message)
    end
  end
  
end
